<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/16
 * Time: 15:00
 */
namespace app\v1\model;

class Curlfunction extends \think\Model
{
    /*
        * 请求接口内容
        * @param  $url : 接口名
        * @param $model : 请求方式 默认get
        * @param $arr  : 请求的参数默认为空
        * */
    public function getApiByName($url, $model = "GET", $arr = "", $result = "arr")
    {
        //echo  $url."model:".$model;
        $curl = curl_init();
        $headers = [
            "access_token:" . '$SUPER_USER_ACCESS_TOKEN$'
            , "content-type: application/json"];
        if ($model == "POST") {
            curl_setopt($curl, CURLOPT_URL, $url);//请求链接
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arr));//请求数据
        } else if ($model == "GET") {
            if (isset($arr['key'])) {

                curl_setopt($curl, CURLOPT_URL, $url . "?" . $arr['key'] . "=" . urlencode($arr['value']));//是否携带参数
                // echo $url . "?" . $arr['key'] . "=" . urlencode($arr['value']);
            } else {
                $arr ? curl_setopt($curl, CURLOPT_URL, $url . "/" . $arr['value']) : curl_setopt($curl, CURLOPT_URL, $url);//是否携带参数
            }
        } else {
            curl_setopt($curl, CURLOPT_URL, $url);//请求链接
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $model);//请求方式
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arr));//请求数据
        }

        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置提交方式

        $data = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);//返回状态码
        curl_close($curl);
        if ($httpCode == "404" || $httpCode == "500" && $result != "arr") {
            $data = $httpCode;
        }
        return $result == "arr" ? $data : $httpCode; //判断返回数据还是状态码
    }

    /*
     * 多参数get方式
     * */
    public function getApiByParam($url, $arr, $result = "arr")
    {
        $curl = curl_init();
        $headers = [
            "access_token:" . '$SUPER_USER_ACCESS_TOKEN$'
            , "content-type: application/json"];
        $path = "";//路径
        for ($i = 0; $i < count($arr); $i++) {
            $path = $path . $arr[$i] . "=" . $arr[$i + 1] . "&";
            $i++;
        };
        curl_setopt($curl, CURLOPT_URL, $url . "?" . $path);//是否携带参数
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);//返回状态码
        curl_close($curl);
        if ($httpCode == "404" || $httpCode == "500") {
            $data = $httpCode;
        }
        return $result == "arr" ? $data : $httpCode; //判断返回数据还是状态码
    }

    //获取Token
    public function getAccessToken()
    {
        //  access_token 应该全局存储与更新，以下代码以写入到文件中做示例
        $path = ROOT_PATH . "/jssdk/access_token.json";
        $data = json_decode(file_get_contents($path));
        if ($data->expire_time < time()) {
            $appid = config("APPID");  //自己的appid
            $appsecret = config("APPSECRET");  //自己的appsecret
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
            $res = json_decode($this->https_request($url));
            $access_token = $res->access_token;
            if ($access_token) {
                $data->expire_time = time() + 7000;
                $data->access_token = $access_token;
                $fp = fopen($path, "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $access_token = $data->access_token;
        }
        return $access_token;
    }

    /*
 * 发送https请求
 * */
    public function https_request($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return 'ERROR ' . curl_error($curl);
        }
        curl_close($curl);
        return $data;
    }
}